﻿#region Using Directives

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections.Generic;

using QuickDev.Common;
using QuickDev.Common.Audit;
using QuickDev.Localization;

using QuickDev.Web.App;
using QuickDev.Web.UI;

using QuickDev.Model;
using QuickDev.Caching;
using QuickDev.BLL;

#endregion

namespace QuickDev.Web.App.Administration
{
    public partial class ReturnTypeEdit : AdminPage
    {
        #region Page Events

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load( object sender , EventArgs e )
        {
            try
            {
                SetProperties();

                if ( !Page.IsPostBack )
                {
                    LoadReturnTypes();
                }
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( ReturnTypeEdit ).Name + ".Page_Load" , ex );
                Master.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        /// <summary>
        /// Handles the Click event of the lbEdit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void lbEdit_Click( object sender , CommandEventArgs e )
        {
            try
            {
                int id = -1;
                if ( int.TryParse( e.CommandArgument.ToString() , out id ) && id > 0 )
                {
                    LoadReturnType( ReturnTypeCache.GetModel( id ) );
                }
            }
            catch ( System.Threading.ThreadAbortException )
            {
                //ignore error.
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( ReturnTypeEdit ).Name + ".lbEdit_Click" , ex );
                Master.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        /// <summary>
        /// Handles the Click event of the lbDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void lbDelete_Click( object sender , CommandEventArgs e )
        {
            try
            {
                int id = 0;
                int.TryParse( e.CommandArgument.ToString() , out id );
                if ( id > 0 )
                {
                    ReturnType model = ReturnTypeCache.GetModel( id );
                    if ( ReturnTypeBLL.Delete( id , Memberships.CurrentUserId ) )
                    {
                        Master.MessageCenter.DisplayFailureMessage( string.Format( LocalizationUtility.GetText( "lblDeleted" ) , model.Name ) );
                        LoadReturnTypes();
                    }
                    else
                    {
                        Master.MessageCenter.DisplaySuccessMessage( string.Format( LocalizationUtility.GetText( "lblDeletedFailed" ) , model.Name ) );
                    }
                }
            }
            catch ( System.Threading.ThreadAbortException )
            {
                //ignore error.
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( ReturnTypeEdit ).Name + ".lbDelete_Click" , ex );
                Master.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click( object sender , EventArgs e )
        {
            if ( Page.IsValid )
            {
                try
                {
                    int id = 0;
                    int.TryParse( lblId.Text , out id );

                    string name = txtName.Text.Trim();
                    string description = txtDescription.Text.Trim();

                    int displayOrder = 0;
                    int.TryParse( txtDisplayOrder.Text.Trim() , out displayOrder );

                    bool published = chkPublished.Checked;

                    ReturnType model = null;
                    if ( id > 0 )
                    {
                        model = ReturnTypeBLL.Update( id , name , description , displayOrder , published , Memberships.CurrentUserId );
                    }
                    else
                    {
                        model = ReturnTypeBLL.Insert( name , description , displayOrder , published , Memberships.CurrentUserId );
                    }

                    if ( model == null )
                    {
                        Master.MessageCenter.DisplaySuccessMessage( string.Format( LocalizationUtility.GetText( "lblNotSaved" ) , typeof( ReturnType ).Name ) );
                    }
                    else
                    {
                        LoadReturnTypes();
                        Master.MessageCenter.DisplaySuccessMessage( string.Format( LocalizationUtility.GetText( "lblSaved" ) , typeof( ReturnType ).Name ) );
                    }
                }
                catch ( System.Threading.ThreadAbortException )
                {
                    //ignore error.
                }
                catch ( Exception ex )
                {
                    Logger.Error( typeof( ReturnTypeEdit ).Name + ".btnSave_Click" , ex );
                    Master.MessageCenter.DisplayCriticalMessage( ex.Message );
                }
            }
        }


        /// <summary>
        /// Handles the Click event of the btnReset control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void btnReset_Click( object sender , EventArgs e )
        {
            Response.Redirect( HttpContext.Current.Request.Url.ToString() );
        }

        /// <summary>
        /// Handles the ItemDataBound event of the dgReturnTypes control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridItemEventArgs"/> instance containing the event data.</param>
        void dgReturnTypes_ItemDataBound( object sender , DataGridItemEventArgs e )
        {
            if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
            {
                LinkButton deleteButton = e.Item.Cells[0].FindControl( "lbDelete" ) as LinkButton;
                if ( deleteButton != null )
                {
                    deleteButton.Attributes.Add( "onclick" , "return confirm(\"" + LocalizationUtility.GetText( "lblConfirmDelete" ) + "\");return false;" );
                }
            }
        }

        #endregion

        #region Methods

        #region Private

        /// <summary>
        /// Sets the ReturnTypes properties.
        /// </summary>
        private void SetProperties()
        {
            this.Title = string.Format( WebUtility.MainTitleTemplate , Master.SiteSettings.SiteName , "退货类型管理" );
        }

        /// <summary>
        /// Loads the ReturnTypes.
        /// </summary>
        private void LoadReturnTypes()
        {
            List<ReturnType> returnTypeCollection = ReturnTypeCache.GetList( true );
            if ( returnTypeCollection.Count > 0 )
            {
                pnlCurrentReturnTypes.Visible = true;
                dgReturnTypes.DataSource = returnTypeCollection;
                dgReturnTypes.ItemDataBound += new DataGridItemEventHandler( dgReturnTypes_ItemDataBound );
                dgReturnTypes.Columns[0].HeaderText = LocalizationUtility.GetText( "hdrEdit" );
                dgReturnTypes.Columns[1].HeaderText = LocalizationUtility.GetText( "hdrName" );
                dgReturnTypes.Columns[2].HeaderText = LocalizationUtility.GetText( "hdrSequence" );
                dgReturnTypes.Columns[3].HeaderText = LocalizationUtility.GetText( "hdrStatus" );
                dgReturnTypes.Columns[4].HeaderText = LocalizationUtility.GetText( "hdrDelete" );
                dgReturnTypes.DataBind();
            }
            else
            {
                pnlCurrentReturnTypes.Visible = false;
            }
        }

        /// <summary>
        /// LoadReturnType( ReturnType model )
        /// </summary>
        /// <param name="model">model</param>
        private void LoadReturnType( ReturnType model )
        {
            lblId.Text = model.Id.ToString();
            txtName.Text = model.Name;
            txtDescription.Text = model.Description;
            chkPublished.Checked = model.Published;
            txtDisplayOrder.Text = model.DisplayOrder.ToString();
        }

        #endregion

        #endregion
    }
}

